using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using PCSComMaterials.Inventory.DS;
using PCSComSale.Order.DS;
using PCSComUtils.Common;
using PCSComUtils.DataAccess;
using PCSComUtils.DataContext;
using PCSComUtils.MasterSetup.DS;
using PCSComUtils.PCSExc;

namespace PCSComSale.Order.BO
{
    public class SOReleaseOrderBO
    {
        private const string This = "PCSComSale.Order.BO.SOReleaseOrderBO";

        public int AddAndReturnID(object pobjMaster, DataSet pdstData, DataSet pdstMasLocCache,
                                  DataSet pdstLocCache, DataSet pdstBinCache, DataSet pdstTransaction)
        {
            var voMaster = (SO_CommitInventoryMasterVO) pobjMaster;
            var dsMaster = new SO_CommitInventoryMasterDS();
            voMaster.CommitInventoryMasterID = dsMaster.AddAndReturnID(voMaster);
            foreach (DataRow dr in pdstData.Tables[0].Rows)
            {
                if (dr.RowState != DataRowState.Deleted)
                    dr[SO_CommitInventoryDetailTable.COMMITINVENTORYMASTERID_FLD] = voMaster.CommitInventoryMasterID;
            }
            var dsDetail = new SO_CommitInventoryDetailDS();
            dsDetail.UpdateDataSet(pdstData);
            return voMaster.CommitInventoryMasterID;
        }

        public void Delete(int pintMasterID, DataSet dstData)
        {
            // subtract commit quantity first
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    foreach (DataRow drData in dstData.Tables[0].Rows)
                    {
                        if (drData.RowState != DataRowState.Deleted)
                        {
                            string strMasLocID = drData[SO_CommitInventoryDetailTable.MASTERLOCATIONID_FLD].ToString();
                            string strLocationID = drData[SO_CommitInventoryDetailTable.LOCATIONID_FLD].ToString();
                            string strBinID = drData[SO_CommitInventoryDetailTable.BINID_FLD].ToString();
                            string strProductID = drData[SO_CommitInventoryDetailTable.PRODUCTID_FLD].ToString();
                            string strCcnid = drData[SO_CommitInventoryDetailTable.CCNID_FLD].ToString();
                            decimal decCommitQty =
                                Convert.ToDecimal(drData[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD]);
                            IV_BinCache objBin = db.IV_BinCaches.SingleOrDefault(
                                e =>
                                e.BinID == Convert.ToInt32(strBinID) &&
                                e.LocationID == Convert.ToInt32(strLocationID) &&
                                e.CCNID == Convert.ToInt32(strCcnid) && e.ProductID == Convert.ToInt32(strProductID));
                            if (objBin != null)
                            {
                                if (objBin.CommitQuantity != null)
                                {
                                    objBin.CommitQuantity = objBin.CommitQuantity - decCommitQty;
                                }
                            }


                            var objLocationCache =
                                db.IV_LocationCaches.SingleOrDefault(
                                    e =>
                                    e.LocationID == Convert.ToInt32(strLocationID) &&
                                    e.MasterLocationID == Convert.ToInt32(strMasLocID) &&
                                    e.CCNID == Convert.ToInt32(strCcnid) && e.ProductID == Convert.ToInt32(strProductID));
                            if (objLocationCache != null)
                            {
                                if (objLocationCache.CommitQuantity != null)
                                {
                                    objLocationCache.CommitQuantity = objLocationCache.CommitQuantity - decCommitQty;
                                }
                            }


                            var objMasLocCache =
                                db.IV_MasLocCaches.SingleOrDefault(
                                    e =>
                                    e.MasterLocationID == Convert.ToInt32(strMasLocID) &&
                                    e.CCNID == Convert.ToInt32(strCcnid) && e.ProductID == Convert.ToInt32(strProductID));
                            if (objMasLocCache != null)
                            {
                                if (objMasLocCache.CommitQuantity != null)
                                {
                                    objMasLocCache.CommitQuantity = objMasLocCache.CommitQuantity - decCommitQty;
                                }
                            }
                        }
                    }
                    // delete detail by master
                    IQueryable<SO_CommitInventoryDetail> listNewData = from objdata in db.SO_CommitInventoryDetails
                                                                       where
                                                                           objdata.CommitInventoryMasterID ==
                                                                           pintMasterID
                                                                       select objdata;
                    db.SO_CommitInventoryDetails.DeleteAllOnSubmit(listNewData);
                    db.SubmitChanges();
                    var commitMaster = db.SO_CommitInventoryMasters.SingleOrDefault(e => e.CommitInventoryMasterID == pintMasterID);
                    if (commitMaster != null)
                    {
                        db.SO_CommitInventoryMasters.DeleteOnSubmit(commitMaster);
                        db.SubmitChanges();
                    }
                }
                trans.Complete();
            }
        }

        public DataRow GetMasterVO(int pintMasterID)
        {
            var dsMaster = new SO_CommitInventoryMasterDS();
            return dsMaster.GetMasterObject(pintMasterID);
        }

        public DataRow GetSOMasterVO(int pintSaleMasterID)
        {
            var dsSaleMaster = new SO_SaleOrderMasterDS();
            return dsSaleMaster.LoadObjectVO(pintSaleMasterID);
        }


        public DataSet List(int masterID)
        {
            var dsCommit = new SO_CommitInventoryDetailDS();
            return dsCommit.List(masterID);
        }

        public DataSet ListBySale(int masterID)
        {
            var dsCommit = new SO_CommitInventoryDetailDS();
            return dsCommit.ListBySale(masterID);
        }

        public DataTable Search(int pintCcnid, DateTime pdtmFromDeliveryDate, DateTime pdtmToDeliveryDate,
                                string pstrOrderNo, string pstrItem, string pstrRevision, string pstrDescription)
        {
            var dsSale = new SO_SaleOrderDetailDS();
            return dsSale.Search(pintCcnid, pdtmFromDeliveryDate, pdtmToDeliveryDate, pstrOrderNo, pstrItem,
                                 pstrRevision, pstrDescription);
        }


        public bool UpdatedRelease(SO_CommitInventoryMaster objMaster, List<SO_CommitInventoryDetail> listDetail,
                                   List<IV_MasLocCache> listMasLocCache, List<IV_LocationCache> listLocCache,
                                   List<IV_BinCache> listBinCache, List<MST_TransactionHistory> listTransactionHistory)
        {
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        if (objMaster.CommitInventoryMasterID > 0)
                        {
                            SO_CommitInventoryMaster objMas =
                                db.SO_CommitInventoryMasters.SingleOrDefault(
                                    e => e.CommitInventoryMasterID == objMaster.CommitInventoryMasterID);
                            objMas.Comment = objMaster.Comment;
                            objMas.CommitDate = objMaster.CommitDate;
                            objMas.CommitmentNo = objMaster.CommitmentNo;
                            objMas.Contact = objMaster.Contact;
                            objMas.DeliveryDate = objMaster.DeliveryDate;
                            objMas.EmployeeID = objMaster.EmployeeID;
                            objMas.SaleOrderMasterID = objMaster.SaleOrderMasterID;
                            objMas.TaxCode = objMaster.TaxCode;
                            objMas.UserName = objMaster.UserName;
                            db.SubmitChanges();
                        }
                        else
                        {
                            db.SO_CommitInventoryMasters.InsertOnSubmit(objMaster);
                            db.SubmitChanges();
                            foreach (SO_CommitInventoryDetail obj in listDetail)
                                obj.CommitInventoryMasterID = objMaster.CommitInventoryMasterID;
                            db.SO_CommitInventoryDetails.InsertAllOnSubmit(listDetail);
                            db.SubmitChanges();
                            foreach (MST_TransactionHistory obj in listTransactionHistory)
                            {
                                List<SO_CommitInventoryDetail> objCommitDetail = (from objDetail in listDetail
                                                                                  where
                                                                                      objDetail.CommitInventoryMasterID ==
                                                                                      objMaster.CommitInventoryMasterID
                                                                                      &&
                                                                                      objDetail.DeliveryScheduleID ==
                                                                                      obj.RefDetailID
                                                                                  select objDetail).ToList();
                                if (objCommitDetail.Count > 0)
                                    obj.RefDetailID = objCommitDetail[0].CommitInventoryDetailID;
                            }
                            db.MST_TransactionHistories.InsertAllOnSubmit(listTransactionHistory);
                            db.IV_MasLocCaches.InsertAllOnSubmit(listMasLocCache);
                            db.IV_LocationCaches.InsertAllOnSubmit(listLocCache);
                            db.IV_BinCaches.InsertAllOnSubmit(listBinCache);
                            db.SubmitChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        Transaction.Current.Rollback(ex);
                        throw;
                    }
                    finally
                    {
                        trans.Complete();
                    }
                    return true;
                }
            }
        }

        public int UpdateRelease(object pobjMaster, DataSet pdstDetail, DataSet pdstMasLocCache,
                                 DataSet pdstLocCache, DataSet pdstBinCache, DataSet pdstTransaction, EnumAction pMode)
        {
            var dsMaster = new SO_CommitInventoryMasterDS();
            var voMaster = (SO_CommitInventoryMasterVO) pobjMaster;
            if (pMode == EnumAction.Add)
            {
                voMaster.CommitInventoryMasterID = dsMaster.AddAndReturnID(voMaster);
                DataSet dstDetail = pdstDetail.Clone();

                #region find all detail object to assign new master id

                foreach (DataRow drowDetail in pdstDetail.Tables[0].Rows)
                {
                    if (drowDetail.RowState != DataRowState.Deleted)
                    {
                        drowDetail[SO_CommitInventoryDetailTable.COMMITINVENTORYMASTERID_FLD] =
                            voMaster.CommitInventoryMasterID;
                        DataRow drowNew = dstDetail.Tables[0].NewRow();
                        drowNew.ItemArray = drowDetail.ItemArray;
                        dstDetail.Tables[0].Rows.Add(drowNew);
                    }
                }

                #endregion

                #region detail & cache data

                // update detail dataset
                var dsCommitDetail = new SO_CommitInventoryDetailDS();
                dsCommitDetail.UpdateDataSet(dstDetail);

                pdstDetail = dsCommitDetail.List(voMaster.CommitInventoryMasterID);

                #region find all transaction history object to assign new master id

                foreach (DataRow drowDetail in pdstTransaction.Tables[0].Rows)
                {
                    if (drowDetail.RowState != DataRowState.Deleted)
                    {
                        drowDetail[MST_TransactionHistoryTable.REFMASTERID_FLD] = voMaster.CommitInventoryMasterID;
                        string strFilter = SO_CommitInventoryDetailTable.COMMITINVENTORYMASTERID_FLD + "=" +
                                           voMaster.CommitInventoryMasterID
                                           + " AND " + SO_CommitInventoryDetailTable.DELIVERYSCHEDULEID_FLD + "=" +
                                           drowDetail[MST_TransactionHistoryTable.REFDETAILID_FLD];
                        drowDetail[MST_TransactionHistoryTable.REFDETAILID_FLD] =
                            pdstDetail.Tables[0].Select(strFilter)[0][
                                SO_CommitInventoryDetailTable.COMMITINVENTORYDETAILID_FLD];
                    }
                }

                #endregion

                // update transaction history
                var dsTransaction = new MST_TransactionHistoryDS();
                dsTransaction.UpdateDataSet(pdstTransaction);
                // update bin cache
                var dsBinCache = new IV_BinCacheDS();
                dsBinCache.UpdateDataSet(pdstBinCache);
                // update location cache
                var dsLocCache = new IV_LocationCacheDS();
                dsLocCache.UpdateDataSet(pdstLocCache);
                // update master location cache
                var dsMasLocCache = new IV_MasLocCacheDS();
                dsMasLocCache.UpdateDataSet(pdstMasLocCache);

                #endregion
            }
            else
                dsMaster.Update(voMaster);
            return voMaster.CommitInventoryMasterID;
        }


        public DataTable GetReportData(int pintMasterID)
        {
            var dsMaster = new SO_CommitInventoryMasterDS();
            return dsMaster.GetReportData(pintMasterID);
        }

        public int InsertOrUpDate(object pobjMaster, DataSet pdstDetail, DataSet pdstMasLocCache,
                                  DataSet pdstLocCache, DataSet pdstBinCache, DataSet pdstTransaction, EnumAction pMode)
        {
            const string methodName = This + ".InsertOrUpDate()";
            var voMaster = (SO_CommitInventoryMasterVO) pobjMaster;
            var objMaster = new SO_CommitInventoryMaster();
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        if (pMode == EnumAction.Add)
                        {
                            #region save master

                            objMaster.CommitmentNo = voMaster.CommitmentNo;
                            if (voMaster.CommitDate != DateTime.MinValue)
                            {
                                objMaster.CommitDate = voMaster.CommitDate;
                            }
                            if (voMaster.SaleOrderMasterID > 0)
                            {
                                objMaster.SaleOrderMasterID = voMaster.SaleOrderMasterID;
                            }
                            objMaster.UserName = voMaster.Username;
                            objMaster.Contact = voMaster.Contact;
                            if (voMaster.DeliveryDate != DateTime.MinValue)
                            {
                                objMaster.DeliveryDate = voMaster.DeliveryDate;
                            }
                            objMaster.Comment = voMaster.Comment;
                            objMaster.TaxCode = voMaster.TaxCode;
                            if (voMaster.EmployeeID > 0)
                            {
                                objMaster.EmployeeID = voMaster.EmployeeID;
                            }
                            if (voMaster.BankID > 0)
                            {
                                objMaster.BankID = voMaster.BankID;
                            }
                            db.SO_CommitInventoryMasters.InsertOnSubmit(objMaster);
                            db.SubmitChanges();

                            #endregion

                            DataSet dstDetail = pdstDetail.Clone();

                            #region find all detail object to assign new master id

                            foreach (DataRow drowDetail in pdstDetail.Tables[0].Rows)
                            {
                                if (drowDetail.RowState != DataRowState.Deleted)
                                {
                                    drowDetail[SO_CommitInventoryDetailTable.COMMITINVENTORYMASTERID_FLD] =
                                        objMaster.CommitInventoryMasterID;
                                    DataRow drowNew = dstDetail.Tables[0].NewRow();
                                    drowNew.ItemArray = drowDetail.ItemArray;
                                    dstDetail.Tables[0].Rows.Add(drowNew);
                                }
                            }

                            #endregion

                            #region detail & cache data

                            // update detail dataset
                            IList<SO_CommitInventoryDetail> listDetail = new List<SO_CommitInventoryDetail>();
                            if (dstDetail.Tables.Count > 0)
                            {
                                #region create new detail

                                foreach (DataRow dr in dstDetail.Tables[0].Rows)
                                {
                                    try
                                    {
                                        var objDetail = new SO_CommitInventoryDetail {CommitInventoryDetailID = 0};
                                        if (dr[SO_CommitInventoryDetailTable.MASTERLOCATIONID_FLD] != DBNull.Value)
                                        {
                                            objDetail.MasterLocationID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.MASTERLOCATIONID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.LINE_FLD] != DBNull.Value)
                                        {
                                            objDetail.Line = Convert.ToInt32(dr[SO_CommitInventoryDetailTable.LINE_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD] != DBNull.Value)
                                        {
                                            objDetail.CommitQuantity =
                                                Convert.ToDecimal(dr[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.AVAILABLEQUANTITY_FLD] != DBNull.Value)
                                        {
                                            objDetail.AvailableQuantity =
                                                Convert.ToDecimal(
                                                    dr[SO_CommitInventoryDetailTable.AVAILABLEQUANTITY_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.COMMITINVENTORYMASTERID_FLD] !=
                                            DBNull.Value)
                                        {
                                            objDetail.CommitInventoryMasterID =
                                                Convert.ToInt32(
                                                    dr[SO_CommitInventoryDetailTable.COMMITINVENTORYMASTERID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.DELIVERYSCHEDULEID_FLD] != DBNull.Value)
                                        {
                                            objDetail.DeliveryScheduleID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.DELIVERYSCHEDULEID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.PRODUCTID_FLD] != DBNull.Value)
                                        {
                                            objDetail.ProductID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.PRODUCTID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.BINID_FLD] != DBNull.Value)
                                        {
                                            objDetail.BinID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.BINID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.LOCATIONID_FLD] != DBNull.Value)
                                        {
                                            objDetail.LocationID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.LOCATIONID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.SELLINGUMID_FLD] != DBNull.Value)
                                        {
                                            objDetail.SellingUMID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.SELLINGUMID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.CCNID_FLD] != DBNull.Value)
                                        {
                                            objDetail.CCNID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.CCNID_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.PRICE_FLD] != DBNull.Value)
                                        {
                                            objDetail.Price =
                                                Convert.ToDecimal(dr[SO_CommitInventoryDetailTable.PRICE_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.STOCKUMID_FLD] != DBNull.Value)
                                        {
                                            objDetail.StockUMID =
                                                Convert.ToInt32(dr[SO_CommitInventoryDetailTable.STOCKUMID_FLD]);
                                        }
                                        listDetail.Add(objDetail);
                                    }
                                    catch
                                    {
                                    }
                                }
                                db.SO_CommitInventoryDetails.InsertAllOnSubmit(listDetail);
                                db.SubmitChanges();

                                #endregion
                            }


                            IQueryable<SO_CommitInventoryDetail> listNewData =
                                from objdata in db.SO_CommitInventoryDetails
                                where objdata.CommitInventoryMasterID == objMaster.CommitInventoryMasterID
                                select objdata;

                            #region find all transaction history object to assign new master id

                            foreach (DataRow drowDetail in pdstTransaction.Tables[0].Rows)
                            {
                                if (drowDetail.RowState != DataRowState.Deleted)
                                {
                                    drowDetail[MST_TransactionHistoryTable.REFMASTERID_FLD] = voMaster.CommitInventoryMasterID;
                                    if (listNewData != null)
                                    {
                                        foreach (SO_CommitInventoryDetail obj in listNewData)
                                        {
                                            if (obj.CommitInventoryMasterID == objMaster.CommitInventoryMasterID &&
                                                obj.DeliveryScheduleID ==
                                                Convert.ToInt32(drowDetail[MST_TransactionHistoryTable.REFDETAILID_FLD]))
                                            {
                                                drowDetail[MST_TransactionHistoryTable.REFDETAILID_FLD] =
                                                    obj.CommitInventoryDetailID;
                                            }
                                        }
                                    }
                                }
                            }

                            #endregion

                            #region Trans

                            IList<MST_TransactionHistory> listTrans = new List<MST_TransactionHistory>();
                            if (pdstTransaction != null && pdstTransaction.Tables.Count > 0)
                            {
                                foreach (DataRow dr in pdstTransaction.Tables[0].Rows)
                                {
                                    var objTrans = new MST_TransactionHistory();
                                    if (dr[MST_TransactionHistoryTable.MASTERLOCATIONID_FLD] != DBNull.Value)
                                    {
                                        objTrans.MasterLocationID = Convert.ToInt32(dr[MST_TransactionHistoryTable.MASTERLOCATIONID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.BINID_FLD] != DBNull.Value)
                                    {
                                        objTrans.BinID = Convert.ToInt32(dr[MST_TransactionHistoryTable.BINID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.BUYSELLCOST_FLD] != DBNull.Value)
                                    {
                                        objTrans.BuySellCost = Convert.ToDecimal(dr[MST_TransactionHistoryTable.BUYSELLCOST_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.INSPSTATUS_FLD] != DBNull.Value)
                                    {
                                        objTrans.InspStatus = Convert.ToByte(dr[MST_TransactionHistoryTable.INSPSTATUS_FLD]);
                                    }
                                    objTrans.TransactionHistoryID = 0;
                                    if (dr[MST_TransactionHistoryTable.TRANSDATE_FLD] != DBNull.Value)
                                    {
                                        objTrans.TransDate = Convert.ToDateTime(dr[MST_TransactionHistoryTable.TRANSDATE_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.POSTDATE_FLD] != DBNull.Value)
                                    {
                                        objTrans.PostDate = Convert.ToDateTime(dr[MST_TransactionHistoryTable.POSTDATE_FLD]);
                                    }
                                    if (objTrans.PostDate == DateTime.MinValue)
                                    {
                                        objTrans.PostDate = null;
                                    }
                                    if (dr[MST_TransactionHistoryTable.REFMASTERID_FLD] != DBNull.Value)
                                    {
                                        objTrans.RefMasterID = Convert.ToInt32(dr[MST_TransactionHistoryTable.REFMASTERID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.REFDETAILID_FLD] != DBNull.Value)
                                    {
                                        objTrans.RefDetailID = Convert.ToInt32(dr[MST_TransactionHistoryTable.REFDETAILID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.COST_FLD] != DBNull.Value)
                                    {
                                        objTrans.StdCost = Convert.ToDecimal(dr[MST_TransactionHistoryTable.COST_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.CCNID_FLD] != DBNull.Value)
                                    {
                                        objTrans.CCNID = Convert.ToInt32(dr[MST_TransactionHistoryTable.CCNID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.TRANTYPEID_FLD] != DBNull.Value)
                                    {
                                        objTrans.TranTypeID = Convert.ToInt32(dr[MST_TransactionHistoryTable.TRANTYPEID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.PARTYID_FLD] != DBNull.Value)
                                    {
                                        objTrans.PartyID = Convert.ToInt32(dr[MST_TransactionHistoryTable.PARTYID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.PARTYLOCATIONID_FLD] != DBNull.Value)
                                    {
                                        objTrans.PartyLocationID = Convert.ToInt32(dr[MST_TransactionHistoryTable.PARTYLOCATIONID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.LOCATIONID_FLD] != DBNull.Value)
                                    {
                                        objTrans.LocationID = Convert.ToInt32(dr[MST_TransactionHistoryTable.LOCATIONID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.PRODUCTID_FLD] != DBNull.Value)
                                    {
                                        objTrans.ProductID =
                                            Convert.ToInt32(dr[MST_TransactionHistoryTable.PRODUCTID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.STOCKUMID_FLD] != DBNull.Value)
                                    {
                                        objTrans.StockUMID = Convert.ToInt32(dr[MST_TransactionHistoryTable.STOCKUMID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.CURRENCYID_FLD] != DBNull.Value)
                                    {
                                        objTrans.CurrencyID = Convert.ToInt32(dr[MST_TransactionHistoryTable.CURRENCYID_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.QUANTITY_FLD] != DBNull.Value)
                                    {
                                        objTrans.Quantity = Convert.ToDecimal(dr[MST_TransactionHistoryTable.QUANTITY_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.MASLOCOHQUANTITY_FLD] != DBNull.Value)
                                    {
                                        objTrans.MasLocOHQuantity = Convert.ToDecimal(dr[MST_TransactionHistoryTable.MASLOCOHQUANTITY_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.LOCATIONOHQUANTITY_FLD] != DBNull.Value)
                                    {
                                        objTrans.LocationOHQuantity = Convert.ToDecimal(dr[MST_TransactionHistoryTable.LOCATIONOHQUANTITY_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.BINOHQUANTITY_FLD] != DBNull.Value)
                                    {
                                        objTrans.BinOHQuantity = Convert.ToDecimal(dr[MST_TransactionHistoryTable.BINOHQUANTITY_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.COMMENT_FLD] != DBNull.Value)
                                    {
                                        objTrans.Comment = dr[MST_TransactionHistoryTable.COMMENT_FLD].ToString();
                                    }
                                    if (dr[MST_TransactionHistoryTable.EXCHANGERATE_FLD] != DBNull.Value)
                                    {
                                        objTrans.ExchangeRate = Convert.ToDecimal(dr[MST_TransactionHistoryTable.EXCHANGERATE_FLD]);
                                    }
                                    if (dr[MST_TransactionHistoryTable.LOT_FLD] != DBNull.Value)
                                    {
                                        objTrans.Lot = dr[MST_TransactionHistoryTable.LOT_FLD].ToString();
                                    }
                                    if (dr[MST_TransactionHistoryTable.SERIAL_FLD] != DBNull.Value)
                                    {
                                        objTrans.Serial = dr[MST_TransactionHistoryTable.SERIAL_FLD].ToString();
                                    }
                                    if (dr[MST_TransactionHistoryTable.USERNAME_FLD] != DBNull.Value)
                                    {
                                        objTrans.UserName = dr[MST_TransactionHistoryTable.USERNAME_FLD].ToString();
                                    }
                                }
                                db.MST_TransactionHistories.InsertAllOnSubmit(listTrans);
                                db.SubmitChanges();
                            }

                            #endregion

                            #region Bin

                            if (pdstBinCache != null && pdstDetail.Tables.Count > 0)
                            {
                                foreach (DataRow dr in pdstDetail.Tables[0].Rows)
                                {
                                    try
                                    {
                                        decimal commitQuantity = 0;
                                        if (dr[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD] != DBNull.Value)
                                        {
                                            commitQuantity = Convert.ToDecimal(dr[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD]);
                                        }
                                        if (dr[SO_CommitInventoryDetailTable.BINID_FLD] != DBNull.Value)
                                        {
                                            var objBin = db.IV_BinCaches.FirstOrDefault(
                                                    e =>
                                                    e.BinID == Convert.ToInt32(dr[SO_CommitInventoryDetailTable.BINID_FLD]) &&
                                                    e.ProductID == Convert.ToInt32(dr[SO_CommitInventoryDetailTable.PRODUCTID_FLD]) &&
                                                    e.CCNID == SystemProperty.CCNID);
                                            if (objBin != null)
                                            {
                                                objBin.CommitQuantity = objBin.CommitQuantity.GetValueOrDefault(0) + commitQuantity;
                                                db.SubmitChanges();
                                            }
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            #endregion

                            #region Locache

                            if (pdstDetail != null && pdstDetail.Tables.Count > 0)
                            {
                                foreach (DataRow dr in pdstDetail.Tables[0].Rows)
                                {
                                    try
                                    {
                                        if (dr["LocationID"] != DBNull.Value)
                                        {
                                            var objLocache = db.IV_LocationCaches.SingleOrDefault(
                                                    e =>
                                                    e.LocationID == Convert.ToInt32(dr["LocationID"]) &&
                                                    e.CCNID == SystemProperty.CCNID &&
                                                    e.ProductID == Convert.ToInt32(dr["ProductID"]));
                                            if (objLocache != null)
                                            {
                                                if (dr[IV_LocationCacheTable.COMMITQUANTITY_FLD] != DBNull.Value)
                                                {
                                                    objLocache.CommitQuantity = objLocache.CommitQuantity.GetValueOrDefault(0) + Convert.ToDecimal(dr[IV_LocationCacheTable.COMMITQUANTITY_FLD]);
                                                    db.SubmitChanges();
                                                }
                                            }
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            #endregion

                            if (pdstDetail != null && pdstDetail.Tables.Count > 0)
                            {
                                foreach (DataRow dr in pdstDetail.Tables[0].Rows)
                                {
                                    try
                                    {
                                        if (dr[IV_MasLocCacheTable.MASTERLOCATIONID_FLD] != DBNull.Value)
                                        {
                                            int iMasterLocationID =
                                                Convert.ToInt32(dr[IV_MasLocCacheTable.MASTERLOCATIONID_FLD]);
                                            int iCCNID = Convert.ToInt32(dr[IV_MasLocCacheTable.CCNID_FLD]);
                                            int iProductID = Convert.ToInt32(dr[IV_MasLocCacheTable.PRODUCTID_FLD]);
                                            var obj = db.IV_MasLocCaches.SingleOrDefault(
                                                    e =>
                                                    e.MasterLocationID == iMasterLocationID && e.CCNID == iCCNID &&
                                                    e.ProductID == iProductID);
                                            if (obj != null)
                                            {
                                                if (dr[IV_MasLocCacheTable.COMMITQUANTITY_FLD] != DBNull.Value)
                                                {
                                                    obj.CommitQuantity = obj.CommitQuantity.GetValueOrDefault(0) + Convert.ToDecimal(dr[IV_MasLocCacheTable.COMMITQUANTITY_FLD]);
                                                }
                                                db.SubmitChanges();
                                            }
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                            }

                            #endregion
                        }
                        else
                        {
                            var obj =
                                db.SO_CommitInventoryMasters.SingleOrDefault(
                                    e => e.CommitInventoryMasterID == voMaster.CommitInventoryMasterID);
                            if (obj != null)
                            {
                                obj.CommitmentNo = voMaster.CommitmentNo;
                                if (voMaster.CommitDate != DateTime.MinValue)
                                {
                                    obj.CommitDate = voMaster.CommitDate;
                                }
                                if (voMaster.DeliveryDate != DateTime.MinValue)
                                {
                                    obj.DeliveryDate = voMaster.DeliveryDate;
                                }
                                obj.Contact = voMaster.Contact;
                                if (voMaster.EmployeeID > 0)
                                {
                                    obj.EmployeeID = voMaster.EmployeeID;
                                }
                                if (voMaster.Username != "")
                                {
                                    obj.UserName = voMaster.Username;
                                }
                                if (voMaster.Comment != "")
                                {
                                    obj.Comment = voMaster.Comment;
                                }
                                if (voMaster.TaxCode != "")
                                {
                                    obj.TaxCode = voMaster.TaxCode;
                                }
                                if (voMaster.SaleOrderMasterID > 0)
                                {
                                    obj.SaleOrderMasterID = voMaster.SaleOrderMasterID;
                                }

                                db.SubmitChanges();
                            }
                        }
                    }
                    catch (PCSBOException ex)
                    {
                        if (ex.mCode == ErrorCode.SQLDUPLICATE_KEYCODE)
                            throw new PCSDBException(ErrorCode.DUPLICATE_KEY, methodName, ex);
                        if (ex.mCode == ErrorCode.MESSAGE_NOT_ENOUGH_COMPONENT_TO_COMPLETE)
                            throw new PCSDBException(ErrorCode.MESSAGE_NOT_ENOUGH_COMPONENT_TO_COMPLETE, methodName, ex);
                        throw new PCSDBException(ErrorCode.ERROR_DB, methodName, ex);
                    }
                }
                trans.Complete();
            }

            return objMaster.CommitInventoryMasterID;
        }
    }

    // end of class
}